2
https://raw.githubusercontent.com/heridho118140173/TUBES-PBO-MASTER/master/TUBES-PBO-master/Bengkel1/src/tb_mobil.java
import java.awt.HeadlessException;
import java.sql.Connection;
import javax.swing.table.DefaultTableModel;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Heridho
 */
public class tb_mobil extends javax.swing.JFrame {
    
    private void kosongkan_foam(){
    txtKodeMobil.setEditable(true); 
    txtKodeMobil.setText(null);
    txtNamaMobil.setText(null);
    txtKodeKonsumen.setText(null);
    txtPlatMobil.setText(null);
    txtKerusakan.setText(null);
    
}
    
    private void tampilkan_data(){
        
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("No.");
        model.addColumn("Kode Mobil");
        model.addColumn("Nama Mobil");
        model.addColumn("Kode Konsumen");
        model.addColumn("Plat Mobil");
        model.addColumn("Kerusakan");
        
        
        try{
            int no = 1;
            String sql = "SELECT * FROM tb_mobil";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.Statement stm = conn.createStatement();
            java.sql.ResultSet res = stm.executeQuery(sql);
            
            while(res.next()){
                model.addRow(new Object[]{no++,res.getString(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5)});
            }
            tabelMobil.setModel(model);
        }catch (SQLException e){
            System.out.println("Error : " + e.getMessage());
        }
        
    }

    /**
     * Creates new form tb_mobil
     */
    public tb_mobil() {
        initComponents();
        kosongkan_foam();
        tampilkan_data();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtKodeMobil = new javax.swing.JTextField();
        txtNamaMobil = new javax.swing.JTextField();
        txtKodeKonsumen = new javax.swing.JTextField();
        txtPlatMobil = new javax.swing.JTextField();
        txtKerusakan = new javax.swing.JTextField();
        btTambah = new javax.swing.JButton();
        btSimpan = new javax.swing.JButton();
        btHapus = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btKeluar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelMobil = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("MOBIL");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(268, 11, -1, -1));

        jLabel2.setText("Kode Mobil");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(29, 92, -1, -1));

        jLabel3.setText("Nama Mobil");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(26, 130, -1, -1));

        jLabel4.setText("Kode Konsumen");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(26, 168, -1, -1));

        jLabel5.setText("Plat Mobil");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(26, 206, -1, -1));

        jLabel6.setText("Kerusakan");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(26, 244, -1, -1));
        getContentPane().add(txtKodeMobil, new org.netbeans.lib.awtextra.AbsoluteConstraints(154, 86, 364, -1));
        getContentPane().add(txtNamaMobil, new org.netbeans.lib.awtextra.AbsoluteConstraints(154, 124, 364, -1));
        getContentPane().add(txtKodeKonsumen, new org.netbeans.lib.awtextra.AbsoluteConstraints(154, 162, 364, -1));
        getContentPane().add(txtPlatMobil, new org.netbeans.lib.awtextra.AbsoluteConstraints(154, 200, 364, -1));
        getContentPane().add(txtKerusakan, new org.netbeans.lib.awtextra.AbsoluteConstraints(154, 238, 364, 40));

        btTambah.setText("Tambah");
        btTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTambahActionPerformed(evt);
            }
        });
        getContentPane().add(btTambah, new org.netbeans.lib.awtextra.AbsoluteConstraints(114, 295, -1, -1));

        btSimpan.setText("Simpan");
        btSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSimpanActionPerformed(evt);
            }
        });
        getContentPane().add(btSimpan, new org.netbeans.lib.awtextra.AbsoluteConstraints(203, 295, -1, -1));

        btHapus.setText("Hapus");
        btHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btHapusActionPerformed(evt);
            }
        });
        getContentPane().add(btHapus, new org.netbeans.lib.awtextra.AbsoluteConstraints(357, 295, -1, -1));

        btEdit.setText("Edit");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });
        getContentPane().add(btEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(288, 295, -1, -1));

        btKeluar.setText("Keluar");
        btKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKeluarActionPerformed(evt);
            }
        });
        getContentPane().add(btKeluar, new org.netbeans.lib.awtextra.AbsoluteConstraints(438, 295, -1, -1));

        tabelMobil.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelMobil.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelMobilMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelMobil);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 336, -1, 106));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKeluarActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btKeluarActionPerformed

    private void btTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTambahActionPerformed
        // TODO add your handling code here:
        kosongkan_foam();
    }//GEN-LAST:event_btTambahActionPerformed

    private void btSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSimpanActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "INSERT INTO tb_mobil VALUES ('"+txtKodeMobil.getText()+"','"+txtNamaMobil.getText()+"','"+txtKodeKonsumen.getText()+"','"+txtPlatMobil.getText()+"','"+txtKerusakan.getText()+"')";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"Simpan data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch (HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
    }//GEN-LAST:event_btSimpanActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "UPDATE tb_mobil SET kode_mobil='"+txtKodeMobil.getText()+"',nama_mobil='"+txtNamaMobil.getText()+"',kode_konsumen='"+txtKodeKonsumen.getText()+"',plat_mobil='"+txtPlatMobil.getText()+"',kerusakan='"+txtKerusakan.getText()+"' WHERE kode_mobil='"+txtKodeMobil.getText()+"'";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"Edit data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch(HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
    }//GEN-LAST:event_btEditActionPerformed

    private void btHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHapusActionPerformed
        // TODO add your handling code here:
        try{
            String sql = "DELETE FROM tb_mobil WHERE kode_mobil='"+txtKodeMobil.getText()+"'";
            java.sql.Connection conn = (Connection)koneksi.getkoneksi();
            java.sql.PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.execute();
            JOptionPane.showMessageDialog(null,"Edit data berhasil..");
            tampilkan_data();
            kosongkan_foam();
        }catch(HeadlessException | SQLException e){
            JOptionPane.showMessageDialog(this,e.getMessage());
        }
    }//GEN-LAST:event_btHapusActionPerformed

    private void tabelMobilMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMobilMouseClicked
        // TODO add your handling code here:
        int baris = tabelMobil.rowAtPoint(evt.getPoint());
        String mb = tabelMobil.getValueAt(baris, 1).toString();
        txtKodeMobil.setText(mb);
        
        String nmb = tabelMobil.getValueAt(baris, 2).toString();
        txtNamaMobil.setText(nmb);
        
        String ksmn = tabelMobil.getValueAt(baris, 3).toString();
        txtKodeKonsumen.setText(ksmn);
        
        String pm = tabelMobil.getValueAt(baris, 4).toString();
        txtPlatMobil.setText(pm);
        
        String krs = tabelMobil.getValueAt(baris, 5).toString();
        txtKerusakan.setText(krs);
        
        
    }//GEN-LAST:event_tabelMobilMouseClicked

    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(tb_mobil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(tb_mobil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(tb_mobil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(tb_mobil.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new tb_mobil().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btHapus;
    private javax.swing.JButton btKeluar;
    private javax.swing.JButton btSimpan;
    private javax.swing.JButton btTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelMobil;
    private javax.swing.JTextField txtKerusakan;
    private javax.swing.JTextField txtKodeKonsumen;
    private javax.swing.JTextField txtKodeMobil;
    private javax.swing.JTextField txtNamaMobil;
    private javax.swing.JTextField txtPlatMobil;
    // End of variables declaration//GEN-END:variables
}
